<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
            <el-form-item label="村社" prop="communityId">
                <el-select v-model="queryParams.communityId" placeholder="请选择评议人所在村社或村" clearable>
                    <el-option
                        v-for="community in communityList"
                        :key="community.communityId"
                        :label="community.communityName"
                        :value="community.communityId"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="匿名" prop="anonymous">
                <el-select v-model="queryParams.anonymous" placeholder="请选择是否匿名" clearable>
                <el-option
                    v-for="item in anonymous"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="类型" prop="anonymous">
                <el-select v-model="queryParams.type" placeholder="请选择评议类型" clearable>
                    <el-option
                        v-for="type in types"
                        :key="type.value"
                        :label="type.label"
                        :value="type.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="consultList" @selection-change="handleSelectionChange" border>
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="评议人" align="center" prop="peopleName" width="100"/>
            <el-table-column label="评议人电话" align="center" prop="mobile" width="200"/>
            <el-table-column label="匿名" align="center" prop="anonymous">
                <template slot-scope="scope">
                    <span v-if="scope.row.anonymous==0">否</span>
                    <span v-if="scope.row.anonymous==1">是</span>
                </template>
            </el-table-column>
            <el-table-column label="村社" align="center" prop="communityName"/>
            <el-table-column label="类型" align="center" prop="type">
                <template slot-scope="scope">
                    <el-tag  v-if="scope.row.type==0">咨询</el-tag>
                    <el-tag type="warning" v-if="scope.row.type==1">建议</el-tag>
                    <el-tag type="danger" v-if="scope.row.type==2">投诉</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="评议内容" align="left" prop="content" width="300">
                <template slot-scope="scope">
                    {{$options.filters.ellipsis(scope.row.content)}}
                </template>
            </el-table-column>
            <el-table-column label="回复内容" align="center" prop="reply">
                <template slot-scope="scope">
                    {{$options.filters.ellipsis(scope.row.reply)}}
                </template>
            </el-table-column>
<!--            <el-table-column label="图片地址" align="center" prop="picUrl" width="100">-->
<!--                <template slot-scope="scope">-->
<!--                    <image-preview :src="scope.row.picUrl" :width="50" :height="50"/>-->
<!--                </template>-->
<!--            </el-table-column>-->
            <el-table-column label="回复状态" align="center" prop="status">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status==0">待回复</el-tag>
                    <el-tag type="danger" v-if="scope.row.status==1">已回复</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="提交时间" align="center" prop="createTime" width="200"/>
            <el-table-column label="操作" align="center" fixed="right"  width="200">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-view"
                        @click="handleRead(scope.row)"
                        v-hasPermi="['organization:consult:edit']"
                    >查看
                    </el-button>
                    <el-button v-if="scope.row.reply==''"
                        size="mini"
                        type="text"
                        icon="el-icon-edit-outline"
                        @click="handleReply(scope.row)"
                        v-hasPermi="['organization:consult:reply']"
                    >回复
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改居民评议对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form"  label-width="130px" >
                <el-form-item label="匿名：" prop="anonymous">
                    <el-tag v-if="form.anonymous==0" disabled>是</el-tag>
                    <el-tag v-if="form.anonymous==1" disabled>否</el-tag>
                </el-form-item>
                <el-form-item label="评议类型：" prop="fromId">
                    <el-radio-group v-model="form.type" disabled>
                        <el-radio v-for="type in types" :label="type.value" :key="type.value">{{type.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="评议人：" prop="fromId"> {{form.peopleName}}</el-form-item>
<!--                <el-form-item label="评议人电话：" prop="fromId"> {{form.communityName}}</el-form-item>-->
                <el-form-item label="所在村社：" prop="communityId">
                    <el-row :span="3">{{form.communityName}}</el-row>
                </el-form-item>
                <el-form-item label="咨询内容：">{{form.content}}</el-form-item>
                <el-form-item label="相关图片：" prop="picUrl">
                    <image-preview :src="form.picUrl" :width="100" :height="100"/>
                </el-form-item>
                <el-form-item label="回复内容：" prop="reply">
                    <el-input v-model="form.reply" type="textarea" placeholder="请输入内容" rows="5" v-if="doType=='reply'"/>
                    <span v-else>
                        <span v-if="form.reply==''">暂未回复</span>
                        <span v-else>{{form.reply}}</span>
                    </span>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button  @click="cancel" v-if="doType=='read' || doType=='reply'">关闭</el-button>
                <el-button type="primary" @click="submitForm" v-if="doType=='reply'">回复</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {listConsult, getConsult, delConsult, addConsult, updateConsult} from "@/api/organization/consult";
import {listCommunity} from "@/api/organization/community";

export default {
    name: "Consult",
    data() {
        return {
            doType:'',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 居民评议表格数据
            consultList: [],
            // 弹出层标题
            title: "",
            open: false,
            anonymous:[{'label':'否','value':0},{'label':'是','value':1}],
            types:[{'label':'咨询','value':0},{'label':'建议','value':1},{'label':'投诉','value':2}],
            communityList: [],
            picUrls:[],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                toId: null,
                fromId: null,
                communityId: null,
                type: null,
                content: null,
                reply: null,
                picUrl: null,
                status: null,
                anonymous: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                fromId: [
                    {required: true, message: "来自谁的提问 关联peop_id不能为空", trigger: "blur"}
                ],
                communityId: [
                    {required: true, message: "评议人所在村社ID不能为空", trigger: "blur"}
                ],
                type: [
                    {required: true, message: "居民评议类型 0：咨询，1：建议，2：投诉不能为空", trigger: "change"}
                ],
                content: [
                    {required: true, message: "咨询内容不能为空", trigger: "blur"}
                ],
                reply: [
                    {required: true, message: "回复内容不能为空", trigger: "blur"}
                ],
                picUrl: [
                    {required: true, message: "图片地址不能为空", trigger: "blur"}
                ],
                status: [
                    {required: true, message: "回复状态 0：待回复，1：已回复不能为空", trigger: "change"}
                ],
                anonymous: [
                    {required: true, message: "是否匿名提交 0：否，1：是不能为空", trigger: "blur"}
                ],
            }
        };
    },
    created() {
        this.getList();
        this.listCommunityOptions();
    },
    //   过滤器
    filters: {
        //   内容超过30个字后以...显示
        ellipsis(value) {
            if (!value) return "";
            if (value.length > 30) {
                //先去掉text里面带来的 &nbsp; 然后再去空格
                return (
                    value
                        .replace(/&nbsp;/g, "")
                        .replace("<br>", "")
                        .replace(/\s/g, "")
                        .slice(0, 30) + "..."
                );
            }
            return value.replace(/\n/g, "<br>");
        }
    },
    methods: {
        listCommunityOptions() {
            listCommunity(this.commonQueryVars).then(resp => {
                this.communityList = resp.rows;
            })
        },
        /** 查询居民评议列表 */
        getList() {
            this.loading = true;
            listConsult(this.queryParams).then(response => {
                this.consultList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                consultId: null,
                toId: null,
                fromId: null,
                communityId: null,
                type: null,
                content: null,
                reply: null,
                picUrl: null,
                status: null,
                anonymous: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.consultId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加居民评议";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const consultId = row.consultId || this.ids
            getConsult(consultId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改居民评议";
            });
        },
        /** 回复 */
        handleReply(row) {
            this.reset();
            const consultId = row.consultId || this.ids
            getConsult(consultId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "回复评议";
                this.picUrls = this.form.picUrl.split(',');
                this.doType='reply';
                this.bizPeople = response.data.bizPeople;
                this.bizCommunity = response.data.bizCommunity;
            });
        },
        /** 查看 */
        handleRead(row) {
            this.reset();
            const consultId = row.consultId || this.ids
            getConsult(consultId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "查看评议";
                this.picUrls = this.form.picUrl.split(',');
                this.doType='read';
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.consultId != null) {
                        updateConsult(this.form).then(response => {
                            let reply = this.form.reply;
                            let id = this.form.consultId;
                            this.form = {};
                            this.form.consultId = id;
                            this.form.reply = reply;
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addConsult(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const consultIds = row.consultId || this.ids;
            this.$modal.confirm('是否确认删除居民评议编号为"' + consultIds + '"的数据项？').then(function () {
                return delConsult(consultIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('organization/consult/export', {
                ...this.queryParams
            }, `consult_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
